<?php
/**
*   +----------------------------------------------------------------------
*   | Author: ONLY <491518132@qq.com>
*	+----------------------------------------------------------------------
*   | Creater Time : 2013-7-19
*   +----------------------------------------------------------------------
*   | Link ( http://www.phpyrb.com  http://www.cloudsskill.com )
*   +----------------------------------------------------------------------
**/
class Article {
	protected $debug;
	function __construct($_debug = false) {
		$this->article = M('Article');
// 		$this->$debug = $_debug;
	}
	
	/**
	 * 返回文章内容
	 * 
	 * @param int $_artid        	
	 * @return array $connent
	 */
	function connent($_artid) {
		$model = M ();
		$field = "a.id,title,type,update_time,content,failure_time";
		$where = '';
		$where = 'a.id ';
		$where .= is_array($_artid) ? count($_artid) > 1 ? ('IN ('.implode($_artid, ',').')') : ('='.$_artid['0']) : ('='.$_artid);
		$sql = "SELECT $field FROM article as a LEFT JOIN article_ext as e ON a.id=e.article_id WHERE $where ORDER BY a.update_time DESC";
		$connent = $model->query($sql);
// 		echo $model->getlastsql ();
		return $connent;
	}
	
	/**
	 * 返回文章基本信息
	 * @param int $_tagid
	 * @param int|array $_artid
	 * @return array $artinfo
	 */
	function artinfo ($_tagid,$_aids) {
		$model = M ();
		$field = "a.id,ob.tags_id tagid,a.title title,a.type,a.status,update_time,ob.sequence";
		$where = "ob.tags_id=".$_tagid." AND a.id IN (".implode(',', $_aids). ")";
		$sql = "SELECT $field FROM `article` a LEFT JOIN tags_obj ob ON a.id=ob.obj_id WHERE $where ORDER BY ob.sequence DESC";
		$artinfo = $model->query($sql);
		return $artinfo;
	}
	
	
	/**
	 * 返回标签下的文章基本信息
	 * @param int $_tagid
	 * @param string $_limit
	 * @param string $_status
	 * @return Ambigous <multitype:, mixed, boolean, multitype:multitype: >
	 */
	function tagtoartinfo($_tagid,$_limit,$_status = TRUE){
		$model = M ();
		$field = "a.id,t.id tagid,title,`status`,a.info,a.type,a.update_time,o.sequence";
		$where = "";
		$where .= is_array($_tagid) ? "t.id IN (".implode(',',$_tagid).")" : "t.id=$_tagid";
		$_status ? $where .= ' AND a.status=1' : false;
		$sql = "SELECT $field FROM `article` as a LEFT JOIN tags_obj as o ON a.id=o.obj_id LEFT JOIN tags as t ON t.id=o.tags_id WHERE $where ORDER BY o.sequence,o.id DESC LIMIT $_limit";
		$artinfo = $model->query($sql);
// 		echo $model->getlastsql ();
// 		exit;
		return $artinfo;
	}
	
	/**
	 * 返回随机文章
	 * @param unknown_type $_uid
	 * @param unknown_type $_cond
	 */
	function roundart ($_uid,$_cond) {
		$model = M('Article');
		$where = array();
		$where['cid'] = $_uid;
		$where['status'] = 1;
		$_cond ? $where['id'] = array('NOT IN',implode(',',$_cond)) : false;
		$art = $model->field('id,title')->where($where)->order('rand()')->limit('0,10')->cache(true,3600)->select();
// 		echo $model->getlastsql ();
		return $art;
	}
	
	function prevnextart($_uid,$_artid){
		$model = M('Article');
		$artinfo = array();
		$artinfo['prev'] = $model->field('id,title')->where("id < $_artid")->order('id DESC')->limit('0,1')->find();
// 		echo $model->getlastsql ();
		$artinfo['next'] = $model->field('id,title')->where("id > $_artid")->order('id ASC')->limit('0,1')->find();
		return $artinfo;
	}
	
	/**
	 * 更改文章状态
	 *
	 * @param
	 *
	 * @return
	 */
	function setarticlesta($user_id,$article_ids,$status) {
		$map['user_id'] = $user_id;
		$map['id'] = $article_ids;
		$res = $this->article->where($map)->setField('status',$status);
		if(empty($res)){ return false; }
		return true;
	}
	
	/**
	 * 返回文章基本信息
	 * @param int|array $_artids
	 * @return array $bassinfo
	 */
	function artbassinfo ($_artids) {
		$where['id'] = is_array($_artids) ? count($_artids) > 1 ? array('IN',implode(',',$_artids)) : $_artids['0'] : $_artids; 
		$model = M('Article');
		$bassinfo = $model->field('id,title,type,update_time')->where($where)->order('update_time DESC')->select();
// 		echo $model->getlastsql ();
		return $bassinfo;
	}
	
	/**
	 * 返回用户文章IDS
	 * 
	 * @param int $_uid        	
	 * @return array $artids
	 */
	function artids($_uid) {
		$model = M ( 'Article' );
		$where = array();
		$where ['uid'] = $_uid;
		$artids = $model->field ( 'id' )->where ( $where )->select ();
		if ($this->debug) {
			echo $model->getlastsql ();
		}
		$artids = $this->_arr2to1 ( $artids );
		return $artids;
	}
	
	/**
	 * 关键字搜索 返回文章IDS
	 * 
	 * @param srting $_keyword        	
	 * @param int $_uid        	
	 * @return array $artids
	 */
	function keyword($_keyword, $_uid = false, $_status = '1') {
		$model = M ( 'Article' );
		$where = array();
		$_uid ? $where ['uid'] = $_uid : false;
		$where ['title'] = array ('like',"%$_keyword%" );
		$where ['status'] = $_status;
		$artids = $model->field ( 'id' )->where ( $where )->select ();
		$artids = $this->_arr2to1 ( $artids );
		return $artids;
	}
	
	/**
	 * 返回帮助文章标签
	 * @param string|int $_uid
	 * @return array $helptag
	 */
	function helptotag ($_uid){
		$model = M('Tags');
		$where = array();
		$where['user_id'] = $_uid;
		$where['code'] = "helps";
		$temp = $model->field('id,name,alias')->where($where)->order('sequence DESC')->limit(4)->select();
		$where = array();
		$model = M();
		foreach ($temp as $k => $v){
			$sql = "SELECT a.id,a.title,a.id as artid,a.cid as uid FROM `tags_obj` as t LEFT JOIN article as a ON t.obj_id=a.id WHERE a.status=1 AND t.tags_id = ".$v['id']." LIMIT 4";
			$art = $model->query($sql);
			$temp[$k]['art'] = $art;
		}
		return $temp;
	}
		
	/**
	 * 返回活动文章IDS
	 * 
	 * @param int $_type        	
	 * @param int $_uid        	
	 * @return array $artids
	 */
	function acttoart($_type, $_uid, $_status = 1) {
		$model = M ( 'Article' );
		$where = array();
		$_uid ? $where ['uid'] = $_uid : false;
		$where ['status'] = $_status;
		$artids = $model->field ( 'id' )->where ( $where )->select ();
		$artids = $this->_arr2to1 ( $artids );
		return $artids;
	}
	
	/**
	 * 地区文章
	 * 
	 * @param array $_condition        	
	 * @param int $_uid        	
	 * @return array $artids
	 */
	function addtoart($_condition, $_uid = false, $_stauts = 1) {
		$model = M ( 'Article' );
		$where = array();
		is_array ( $_condition ) ? $where = $_condition : false;
		$_uid ? $where ['uid'] = $_uid : false;
		$where ['status'] = $_stauts;
		$artids = $model->field ( 'id' )->where ( $where )->select ();
		$artids = $this->_arr2to1 ( $artids );
		return $artids;
	}
	
	/**
	 * 更新点击率
	 * 
	 * @param 文章ID $_artid        	
	 * @return array $reid
	 */
	function hots($_artid) {
		$model = M ( 'Article' );
		$where = array();
		$where ['id'] = $_artid;
		$reid = $model->where ( $where )->setInc ( 'clicks' );
		if ($this->Debug) {
			echo ($model->getLastSql ());
		}
		return $reid;
	}
	
	/**
	 * 添加、修改
	 * 
	 * @param array $_data
	 *        	 数据
	 * @param string $_model
	 *        	模型
	 * @param string $_type
	 *        	动作名
	 */
	function add_updata($_data, $_model = 'Article', $_type = 'add') {
		$model = M ( "$_model" );
		if ($_data) {
			$reid = $model->create ( $_data );
			if ($reid) {
				if ($_type == 'add') {
					$reid = $model->add ();
					if ($this->Debug) echo ( $model->getLastSql () );
					return $reid;
				} elseif ($_type == 'updata') {
					$reid = $model->save ();
// 					echo $model->getLastSql ();//exit();
					return $reid;
				} else {
					$this->_message ( false, '请传入正确的动作！', __LINE__ );
				}
			} else {
				$this->_message ( false, "创建模型失败！", __LINE__ );
			}
		} else {
			$this->_message ( false, '没有数据！', __LINE__ );
		}
	}
	
	/**
	 * 根据ID删除数据
	 * 
	 * @param string|array $_id        	
	 */
	function delete($_id,$_model = 'Article') {
		$model = M ( "$_model" );
		if ($_id) {
			$where ['id'] = is_array ( $_id ) ? array ('IN',implode ( ',', $_id )) : $_id;
			$reid = $model->where ( $where )->delete ();
			return $reid;
		} else {
			return false;
		}
	}
	
	/**
	 * 返回总条数
	 * @param int $_cateid
	 * @return int $count
	 */
	function count($_model,$_map){
		$model = M("$_model");
		$count = $model->where($_map)->count('id');
		return $count;
	}
	
	
	/**
	 * 返回报价列表
	 * @param int|string $_cateid
	 * @param string $_limit
	 * @return array $pricelist
	 */
	function quotation($_cateid ,$_limit){
		$model = M('Download');
		$where['cateid'] = $_cateid == 'sj' || $_cateid != 39 ? array('NEQ',39) : 39;
		$field = "id,title,catid,updatetime,file";
		$where['status'] = 1;
		$pricelist = $model->field($field)->where($where)->order('updatetime DESC')->limit($_limit)->select();
		return $pricelist;
	}
	
	public function downloads($_path,$_name){
		if (!file_exists($_path)){
			header("Content-type: text/html; charset=utf-8");
			echo "File not found!";
			exit;
		} else {
			$file = fopen($_path,"r");
			Header("Content-type: application/octet-stream");
			Header("Accept-Ranges: bytes");
			Header("Accept-Length: ".filesize($_path));
			Header("Content-Disposition: attachment; filename=51efa22134001.xls");
			echo fread($file, filesize($_path));
			fclose($file);
		}
	}
	
	public function down($_name){
		$name_tmp = explode("_",$_name);
		$type = $name_tmp[0];
		$file_time = explode(".",$name_tmp[3]);
		$file_time = $file_time[0];
		$file_date = date("Y/md",$file_time);
		$file_dir = SITE_PATH."/data/uploads/$type/$file_date/";
	
		if (!file_exists($file_dir.$_name)){
		header("Content-type: text/html; charset=utf-8");
				echo "File not found!";
						exit;
		} else {
		$file = fopen($file_dir.$_name,"r");
		Header("Content-type: application/octet-stream");
			Header("Accept-Ranges: bytes");
			Header("Accept-Length: ".filesize($file_dir . $_name));
				Header("Content-Disposition: attachment; filename=51efa22134001.xls");
				echo fread($file, filesize($file_dir.$_name));
				fclose($file);
		}
	}
	
	/**
	 * 返回提示信息
	 * 
	 * @param multitype $_val        	
	 * @param
	 *        	json
	 */
	function _message($_val, $_msg, $_line = false) {
		if (! $_val || empty ( $_val ) || $_val === 'false') {
			if ($this->Debug) {
				$errormsg = '错误提示：' . $_msg . '在第' . "$_line" . '行！';
				exit ( $errormsg );
			} else {
				return 'false';
				exit ();
			}
		} else {
			return 'true';
			exit ();
		}
	}
	
	/**
	 * 二维数组返回一维或字符串
	 * 
	 * @param array $_arr        	
	 * @param string $_field        	
	 * @param string $_type
	 *        	1为数组 2为字符串
	 * @return multitype:array multitype:string
	 */
	function _arr2to1($_arr, $_field = 'id', $_type = '1') {
		if (! $_arr)
			return array ();
		$arrayids = array ();
		foreach ( $_arr as $k => $v ) {
			$arrayids [$k] = $v [$_field];
		}
		if ($_type != '1')
			$arrayids = implode ( ',', $arrayids );
		return $arrayids;
	}
	
	
}